package com.ruoyi.biz.category.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.biz.category.cache.CategoryCache;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.BizCategory;
import com.ruoyi.biz.category.service.IBizCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 分类Controller
 *
 * @author Fei.Cong
 * @date 2024-07-11
 */
@RestController
@RequestMapping("/category/category")
public class BizCategoryController extends BaseController {

    @Autowired
    private IBizCategoryService bizCategoryService;

    @Autowired
    private CategoryCache categoryCache;

    /**
     * 查询分类列表
     */
    @PreAuthorize("@ss.hasPermi('category:category:list')")
    @GetMapping("/list")
    public AjaxResult list(BizCategory bizCategory) {
        LambdaQueryWrapper<BizCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return success(bizCategoryService.list(lambdaQueryWrapper));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(BizCategory bizCategory) {
        LambdaQueryWrapper<BizCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(BizCategory::getOrderNum);
        List<BizCategory> menus = bizCategoryService.list(lambdaQueryWrapper);
        return success(bizCategoryService.buildMenuTreeSelect(menus));
    }

    /**
     * 分页查询分类列表
     */
    @PreAuthorize("@ss.hasPermi('category:category:list')")
    @GetMapping("/page")
    public TableDataInfo page(BizCategory bizCategory) {
        return getDataTable(bizCategoryService.pageList(bizCategory, startPage()));
    }

    /**
     * 导出分类列表
     */
    @PreAuthorize("@ss.hasPermi('category:category:export')")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizCategory bizCategory) {
        List<BizCategory> list = bizCategoryService.queryList(bizCategory);
        ExcelUtil<BizCategory> util = new ExcelUtil<BizCategory>(BizCategory.class);
        util.exportExcel(response, list, "分类数据");
    }

    /**
     * 获取分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('category:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizCategoryService.getById(id));
    }

    /**
     * 新增分类
     */
    @PreAuthorize("@ss.hasPermi('category:category:add')")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizCategory bizCategory) {
        return toAjax(bizCategoryService.save(bizCategory));
    }

    /**
     * 修改分类
     */
    @PreAuthorize("@ss.hasPermi('category:category:edit')")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizCategory bizCategory) {
        return toAjax(bizCategoryService.updateById(bizCategory));
    }

    /**
     * 删除分类
     */
    @PreAuthorize("@ss.hasPermi('category:category:remove')")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizCategoryService.removeBatchByIds(ids));
    }

    /**
     * 刷新缓存
     */
    @Log(title = "刷新缓存", businessType = BusinessType.CLEAN)
    @GetMapping("/refreshCache")
    public R refreshCache() {
        categoryCache.clearCache();
        categoryCache.loadCache();
        return R.ok();
    }
}
